/**
 * Project Name:youba
 * Package Name:com.inoneone.youba.mcht.service.impl
 * Date:2017/6/22下午12:11
 * Copyright (c) 2017, www.zbj.com All Rights Reserved
 */
package com.inoneone.youba.mcht.service.impl;

import com.inoneone.youba.mcht.dao.mapper.*;
import com.inoneone.youba.mcht.dao.po.*;
import com.inoneone.youba.mcht.dto.rsp.MchChargeStatisticsRsp;
import com.inoneone.youba.mcht.dto.rsp.MchFuelStatisticsRsp;
import com.inoneone.youba.mcht.enums.ErrorCode;
import com.inoneone.toolkit.exception.BusinessRuntimeException;
import com.inoneone.youba.mcht.service.DataApi;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Description: (描述) </br>
 * Date: 2017/6/22下午12:11 </br>
 *
 * @author 姬远玄
 * @since JDK 1.7
 */
@Service
public class DataApiImpl implements DataApi {

    @Autowired
    MchFuelStatisticsMapper mchFuelStatisticsMapper;

    @Autowired
    TankerFuelStatisticsMapper tankerFuelStatisticsMapper;

    @Autowired
    CardFuelStatisticsMapper cardFuelStatisticsMapper;

    @Autowired
    CardChargeStatisticsMapper cardChargeStatisticsMapper;

    @Autowired
    MchCardChargeStatisticsMapper mchCardChargeStatisticsMapper;

    /**
     * Description: 查询一段时间加油数据</br>
     * Date: 2017-06-22 12:35:58</br>
     *
     * @param
     * @return
     * @author 姬远玄
     * @since JDK 1.7
    */
    @Override
    public MchFuelStatisticsRsp queryMchData(String mchId, Date beginDate, Date endDate, boolean desc)
    {
        //日期长度不能超过3个月
        checkDateLength(beginDate, endDate);

        //查询数据
        MchFuelStatisticsExample mchFuelStatisticsExample = new MchFuelStatisticsExample();
        mchFuelStatisticsExample.createCriteria()
                .andOrderDateBetween(beginDate, endDate)
                .andMchIdEqualTo(mchId);
        if(desc)
        {
            mchFuelStatisticsExample.setOrderByClause("order_date desc");

        }
        else {
            mchFuelStatisticsExample.setOrderByClause("order_date asc");
        }
        List<MchFuelStatistics> mchFuelStatistiicsList = mchFuelStatisticsMapper.selectByExample(mchFuelStatisticsExample);

        MchFuelStatisticsRsp mchFuelStatisticsRsp = new MchFuelStatisticsRsp();
        mchFuelStatisticsRsp.setBeginDate(beginDate);
        mchFuelStatisticsRsp.setEndDate(endDate);

        //拼装返回数据
        List<MchFuelStatisticsRsp.Statistics> statisticsRspList = new ArrayList<>();
        for(MchFuelStatistics mchFuelStatistics : mchFuelStatistiicsList)
        {
            //复制
            MchFuelStatisticsRsp.Statistics statistics = new MchFuelStatisticsRsp.Statistics();
            BeanUtils.copyProperties(mchFuelStatistics, statistics);
            statisticsRspList.add(statistics);

            //统计
            mchFuelStatisticsRsp.setDateTotalAmount(mchFuelStatisticsRsp.getDateTotalAmount().add(mchFuelStatistics.getTotalAmount()));
            mchFuelStatisticsRsp.setDateTotalFuel(mchFuelStatisticsRsp.getDateTotalFuel().add(mchFuelStatistics.getTotalFuel()));
            mchFuelStatisticsRsp.setDateTotalCount(mchFuelStatisticsRsp.getDateTotalCount() + mchFuelStatistics.getTotalCount());
        }
        mchFuelStatisticsRsp.setStatistics(statisticsRspList);
        return mchFuelStatisticsRsp;

    }

    @Override
    public MchFuelStatisticsRsp queryTankerData(String tankerId, Date beginDate, Date endDate, boolean desc)
    {
        //日期长度不能超过3个月
        checkDateLength(beginDate, endDate);

        //查询数据
        TankerFuelStatisticsExample tankerFuelStatisticsExample = new TankerFuelStatisticsExample();
        tankerFuelStatisticsExample.createCriteria()
                .andOrderDateBetween(beginDate, endDate)
                .andTankerIdEqualTo(tankerId);
        if(desc)
        {
            tankerFuelStatisticsExample.setOrderByClause("order_date desc");

        }
        else {
            tankerFuelStatisticsExample.setOrderByClause("order_date asc");
        }
        List<TankerFuelStatistics> tankerFuelStatisticsList = tankerFuelStatisticsMapper.selectByExample(tankerFuelStatisticsExample);

        MchFuelStatisticsRsp mchFuelStatisticsRsp = new MchFuelStatisticsRsp();
        mchFuelStatisticsRsp.setBeginDate(beginDate);
        mchFuelStatisticsRsp.setEndDate(endDate);

        //拼装返回数据
        List<MchFuelStatisticsRsp.Statistics> statisticsRspList = new ArrayList<>();
        for(TankerFuelStatistics tankerFuelStatistics : tankerFuelStatisticsList)
        {
            //复制
            MchFuelStatisticsRsp.Statistics statistics = new MchFuelStatisticsRsp.Statistics();
            BeanUtils.copyProperties(tankerFuelStatistics, statistics);
            statisticsRspList.add(statistics);

            //统计
            mchFuelStatisticsRsp.setDateTotalAmount(mchFuelStatisticsRsp.getDateTotalAmount().add(tankerFuelStatistics.getTotalAmount()));
            mchFuelStatisticsRsp.setDateTotalFuel(mchFuelStatisticsRsp.getDateTotalFuel().add(tankerFuelStatistics.getTotalFuel()));
            mchFuelStatisticsRsp.setDateTotalCount(mchFuelStatisticsRsp.getDateTotalCount() + tankerFuelStatistics.getTotalCount());
        }
        mchFuelStatisticsRsp.setStatistics(statisticsRspList);
        return mchFuelStatisticsRsp;

    }

    @Override
    public MchFuelStatisticsRsp queryCardData(String cardId, Date beginDate, Date endDate, boolean desc)
    {
        //日期长度不能超过3个月
        checkDateLength(beginDate, endDate);


        //查询数据
        CardFuelStatisticsExample cardFuelStatisticsExample = new CardFuelStatisticsExample();
        cardFuelStatisticsExample.createCriteria()
                .andOrderDateBetween(beginDate, endDate)
                .andCardIdEqualTo(cardId);
        if(desc)
        {
            cardFuelStatisticsExample.setOrderByClause("order_date desc");

        }
        else {
            cardFuelStatisticsExample.setOrderByClause("order_date asc");
        }
        List<CardFuelStatistics> cardFuelStatisticsList = cardFuelStatisticsMapper.selectByExample(cardFuelStatisticsExample);

        MchFuelStatisticsRsp mchFuelStatisticsRsp = new MchFuelStatisticsRsp();
        mchFuelStatisticsRsp.setBeginDate(beginDate);
        mchFuelStatisticsRsp.setEndDate(endDate);

        //拼装返回数据
        List<MchFuelStatisticsRsp.Statistics> statisticsRspList = new ArrayList<>();
        for(CardFuelStatistics cardFuelStatistics : cardFuelStatisticsList)
        {
            //复制
            MchFuelStatisticsRsp.Statistics statistics = new MchFuelStatisticsRsp.Statistics();
            BeanUtils.copyProperties(cardFuelStatistics, statistics);
            statisticsRspList.add(statistics);

            //统计
            mchFuelStatisticsRsp.setDateTotalAmount(mchFuelStatisticsRsp.getDateTotalAmount().add(cardFuelStatistics.getTotalAmount()));
            mchFuelStatisticsRsp.setDateTotalFuel(mchFuelStatisticsRsp.getDateTotalFuel().add(cardFuelStatistics.getTotalFuel()));
            mchFuelStatisticsRsp.setDateTotalCount(mchFuelStatisticsRsp.getDateTotalCount() + cardFuelStatistics.getTotalCount());
        }
        mchFuelStatisticsRsp.setStatistics(statisticsRspList);
        return mchFuelStatisticsRsp;

    }

    /**
     * Description: 查询ic卡充值数据</br>
     * Date: 2017-06-26 11:22:57</br>
     *
     * @param cardId
     * @param beginDate
     * @param endDate   @return
     * @author 姬远玄
     * @since JDK 1.7
     */
    @Override
    public MchChargeStatisticsRsp queryChargeData(String cardId, Date beginDate, Date endDate, boolean desc) {
        //日期长度不能超过三个月
        checkDateLength(beginDate, endDate);

        //查询数据
        CardChargeStatisticsExample cardChargeStatisticsExample = new CardChargeStatisticsExample();
        CardChargeStatisticsExample.Criteria criteria = cardChargeStatisticsExample.createCriteria();
        criteria.andOrderDateBetween(beginDate, endDate).andCardIdEqualTo(cardId);

        if(desc)
        {
            cardChargeStatisticsExample.setOrderByClause("order_date desc");

        }
        else {
            cardChargeStatisticsExample.setOrderByClause("order_date asc");
        }
        List<CardChargeStatistics> cardChargeStatisticsList = cardChargeStatisticsMapper.selectByExample(cardChargeStatisticsExample);

        MchChargeStatisticsRsp mchChargeStatisticsRsp = new MchChargeStatisticsRsp();
        mchChargeStatisticsRsp.setBeginDate(beginDate);
        mchChargeStatisticsRsp.setEndDate(endDate);

        //拼装返回数据
        List<MchChargeStatisticsRsp.Statistics> statisticsRspList = new ArrayList<>();
        for(CardChargeStatistics cardChargeStatistics : cardChargeStatisticsList)
        {
            //复制
            MchChargeStatisticsRsp.Statistics statistics = new MchChargeStatisticsRsp.Statistics();
            BeanUtils.copyProperties(cardChargeStatistics, statistics);
            statisticsRspList.add(statistics);

            //统计
            mchChargeStatisticsRsp.setDateTotalAmount(mchChargeStatisticsRsp.getDateTotalAmount().add(cardChargeStatistics.getTotalAmount()));
            mchChargeStatisticsRsp.setDateTotalCount(mchChargeStatisticsRsp.getDateTotalCount() + cardChargeStatistics.getTotalCount());
        }
        mchChargeStatisticsRsp.setStatistics(statisticsRspList);
        return mchChargeStatisticsRsp;
    }

    /**
     * 商户维度充值数据
     * @param beginDate
     * @param endDate
     * @return
     */
    @Override
    public MchChargeStatisticsRsp queryMchChargeData(String mchId, Date beginDate, Date endDate, boolean desc) {
        //日期长度不能超过三个月
        checkDateLength(beginDate, endDate);

        //查询数据
        MchCardChargeStatisticsExample mchCardChargeStatisticsExample = new MchCardChargeStatisticsExample();
        mchCardChargeStatisticsExample.createCriteria()
        .andMchIdEqualTo(mchId)
        .andOrderDateBetween(beginDate, endDate);

        if(desc)
        {
            mchCardChargeStatisticsExample.setOrderByClause("order_date desc");

        }
        else {
            mchCardChargeStatisticsExample.setOrderByClause("order_date asc");
        }
        List<MchCardChargeStatistics> cardChargeStatisticsList = mchCardChargeStatisticsMapper.selectByExample(mchCardChargeStatisticsExample);

        MchChargeStatisticsRsp mchChargeStatisticsRsp = new MchChargeStatisticsRsp();
        mchChargeStatisticsRsp.setBeginDate(beginDate);
        mchChargeStatisticsRsp.setEndDate(endDate);

        //拼装返回数据
        List<MchChargeStatisticsRsp.Statistics> statisticsRspList = new ArrayList<>();
        for(MchCardChargeStatistics cardChargeStatistics : cardChargeStatisticsList)
        {
            //复制
            MchChargeStatisticsRsp.Statistics statistics = new MchChargeStatisticsRsp.Statistics();
            BeanUtils.copyProperties(cardChargeStatistics, statistics);
            statisticsRspList.add(statistics);

            //统计
            mchChargeStatisticsRsp.setDateTotalAmount(mchChargeStatisticsRsp.getDateTotalAmount().add(cardChargeStatistics.getTotalAmount()));
            mchChargeStatisticsRsp.setDateTotalCount(mchChargeStatisticsRsp.getDateTotalCount() + cardChargeStatistics.getTotalCount());
        }
        mchChargeStatisticsRsp.setStatistics(statisticsRspList);
        return mchChargeStatisticsRsp;
    }

    /**
     * 检验日期长度
     * @param beginDate
     * @param endDate
     */
    private void checkDateLength(Date beginDate, Date endDate)
    {
        int days = (int) ((beginDate.getTime() - endDate.getTime()) / (1000*3600*24));
        if(days > 93)
        {
            throw new BusinessRuntimeException(ErrorCode.COUNT_BAD_DATE);
        }

    }
}
